package semaforos;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class SemaforoPeatones extends JFrame implements Observer{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel panelSemaforo = null;
	private JLabel contLabel = null;
	private LuzSemaforo luzVerde = null;
	private LuzSemaforo luzRoja = null;
	private JButton pedirPasoButton = null;
	private Controlador controlador = null;

	/**
	 * This is the default constructor
	 */
	public SemaforoPeatones() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(120, 555);
		this.setContentPane(getJContentPane());
		this.setTitle("Peatones");
		GestorSemaforo.getGestorSemaforo().addObserver(this);
		addWindowListener(getControlador());
		setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPanelSemaforo(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes panelSemaforo	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelSemaforo() {
		if (panelSemaforo == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(4);
			gridLayout.setVgap(10);
			gridLayout.setColumns(1);
			panelSemaforo = new JPanel();
			panelSemaforo.setLayout(gridLayout);
			panelSemaforo.add(getContLabel(), null);
			panelSemaforo.add(getLuzRoja(), null);
			panelSemaforo.add(getLuzVerde(),null);
			panelSemaforo.add(getPedirPasoButton(), null);
		}
		return panelSemaforo;
	}
	
	private LuzSemaforo getLuzVerde() {
		if (luzVerde == null) {
			luzVerde = new LuzSemaforo("verde",false);
		}
		return luzVerde;
	}

	private LuzSemaforo getLuzRoja() {
		if (luzRoja == null) {
			luzRoja=new LuzSemaforo("rojo", true);
		}
		return luzRoja;
	}

	private JLabel getContLabel() {
		if (contLabel == null) {
			contLabel= new JLabel();
			contLabel.setText("15");
			contLabel.setForeground(Color.red);
			contLabel.setHorizontalTextPosition(SwingConstants.CENTER);
			contLabel.setHorizontalAlignment(SwingConstants.CENTER);
			contLabel.setBackground(Color.black);
			contLabel.setOpaque(true);
			contLabel.setFont(new Font("Lucida Grande", Font.BOLD, 72));
		}
		return contLabel;
	}

	/**
	 * This method initializes pedirPasoButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPedirPasoButton() {
		if (pedirPasoButton == null) {
			pedirPasoButton = new JButton();
			pedirPasoButton.setText("Pedir Paso");
			pedirPasoButton.addActionListener(getControlador());
		}
		return pedirPasoButton;
	}

	@Override
	public void update(Observable o, Object arg) {
		GestorSemaforo gS = GestorSemaforo.getGestorSemaforo();
		boolean estaVerde = gS.estaVerde();
		if (estaVerde) {
			getContLabel().setForeground(Color.GREEN);
		}
		else {
			getContLabel().setForeground(Color.RED);
		}
		getLuzVerde().setActive(estaVerde);
		getLuzRoja().setActive(!estaVerde);
		getContLabel().setText(String.valueOf(gS.getContador()));
		getPedirPasoButton().setEnabled(!estaVerde);
	}
	
	private Controlador getControlador() {
		if (controlador == null) {
			controlador = new Controlador();
		}
		return controlador;
	}
	
	private class Controlador extends WindowAdapter implements ActionListener {

		@Override
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			GestorSemaforo.getGestorSemaforo().ponerVerde();
		}
		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
